//
//  PopoverPresentationController.swift
//  DSWeibo
//
//  Created by J on 16/2/22.
//  Copyright © 2016年 J. All rights reserved.
//

import UIKit


//@available(iOS 8.0, *)
class PopoverPresentationController: UIPresentationController {

    //定义属性保存菜单大小
    var presentFrame = CGRectZero
    ///  用于创建负责转场动画的对象
    ///
    ///  - parameter presentedViewController:  被展现的控制器
    ///  - parameter presentingViewController: 发起的控制器 Xcode6是nil Xcode7时野指针
    ///
    ///  - returns: 负责转场动画的对象
    override init(presentedViewController: UIViewController, presentingViewController: UIViewController) {
     
        super.init(presentedViewController: presentedViewController, presentingViewController: presentingViewController)
        
//        print(presentedViewController)
//        print(presentingViewController)
        

    }
    
    //即将布局转场子视图时调用
    override func containerViewWillLayoutSubviews(){
        
        //修改弹出视图的大小
//        containerView 容器视图
//        presentedView() 被展现视图
        if presentFrame == CGRectZero{
        presentedView()?.frame = CGRect(x:100,y:60, width: 200, height: 200)
        }else{
            
            presentedView()?.frame = presentFrame
        }
        presentedView()?.backgroundColor = UIColor.clearColor()
        //2.在容器视图上添加一个蒙板,插入到展现视图的下面
        //因为展现视图和蒙板都在一个仕途上,而后添加的盖住先添加的
        containerView?.insertSubview(coverView, atIndex: 0)
    }
    
    
    //MARK: 懒加载
    private lazy var coverView:UIView = {
        //1.创建View
       let view = UIView()
        view.backgroundColor = UIColor.clearColor()
        view.frame = UIScreen.mainScreen().bounds
        //2.添加监听
        let tap = UITapGestureRecognizer(target: self, action: #selector(PopoverPresentationController.close))
        view.addGestureRecognizer(tap)
        return view

        
    }()
    
    func close(){
        //拿到当前弹出控制器
        presentedViewController.dismissViewControllerAnimated(true, completion: nil)
        
    }
}
